package mwa.cs.dao;

import java.math.BigDecimal;
import java.util.Map;
import mwa.cs.database.Database;
import mwa.cs.model.ChargeRate;

/**
 * A class used for managing charge_rate
 */
public class ChargeRateDAO {
	
	/** a database of this charge_rate */
    private Database db;
    
    /**
     * Instantiate a new object of this class
     * @param database a database
     */
    public ChargeRateDAO(Database db) {
        this.db = db;
    }
    
    /**
     * finding charge_rate by site id
     * @param pnSiteId that want to find charge rate
     * @return charge_rate in this site
     */
    public ChargeRate findChargeRateBySiteId(int pnSiteId) {
    	String vaSql = "SELECT * FROM charge_rate WHERE site_id = ?";
    	Map<String, Object> result = db.querySingle(vaSql, pnSiteId);
        if (result != null) {
        	// GarbageCharge, WasteWaterCharge, OtherCharge is not to be "null" if it is null, error will be occur
        	String vaGarbageCharge = ((BigDecimal) result.get("GARBAGE_CHARGE")).toString();
        	String vaWasteWaterCharge = ((BigDecimal) result.get("WASTEWATER_CHARGE")).toString();
        	String vaOtherCharge = ((BigDecimal) result.get("OTHER_CHARGE")).toString();
        	int vnGarbageCharge = 0, vnWasteWaterCharge = 0, vnOtherCharge = 0;
        	if (vaGarbageCharge != null) vnGarbageCharge = Integer.parseInt(vaGarbageCharge);
        	if (vaWasteWaterCharge != null) vnWasteWaterCharge = Integer.parseInt(vaWasteWaterCharge);
        	if (vaOtherCharge != null) vnOtherCharge = Integer.parseInt(vaOtherCharge);
        	ChargeRate voChargeRate = new ChargeRate(pnSiteId, 
        			vnGarbageCharge,
        			vnWasteWaterCharge,
        			vnOtherCharge);
            return voChargeRate;
        } else {
            return null;
        }
    }
    
    public Map<String, Object> getChargeRateBySiteId(int pnSiteId) {
    	String vaSql = "SELECT garbage_charge,wastewater_charge,other_charge FROM charge_rate WHERE site_id = ?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId);
        if (result != null) {
        	return result;
        } else {
            return null;
        }
    }

}
